package com.the_ring.chapter12.widget;

import android.view.animation.Animation;
import android.view.animation.Transformation;

public class SwingAnimation extends Animation {

    private float mMiddleDegrees, mLeftDegrees, mRightDegrees;
    private float mPivotX, mPivotY;

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float degrees;
        float leftPos = (float) (1.0 / 4.0);    // 摆到左边端点时的时间比例
        float rightPos = (float) (3.0 / 4.0);   // 摆到右边端点时的时间比例
        if (interpolatedTime <= leftPos) {      // 从中间线往左边端点摆
            degrees = mMiddleDegrees + (mLeftDegrees - mMiddleDegrees) * interpolatedTime;
        } else if (interpolatedTime > leftPos && interpolatedTime < rightPos) {
            // 从左端点往右端点摆
            degrees = mLeftDegrees + ((mRightDegrees - mLeftDegrees) * (interpolatedTime - leftPos) * 2);
        } else {
            // 从右端点往中间线摆
            degrees = mRightDegrees + ((mMiddleDegrees - mRightDegrees) * (interpolatedTime - rightPos) * 4);
        }
        float scale = getScaleFactor();  // 获得缩放率
        if (mPivotX == 0.0f && mPivotY == 0.0f) {
            t.getMatrix().setRotate(degrees);
        } else {
            t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
        }
    }
}
